package com.xiaoyong.system.controller;

import com.xiaoyong.entity.Result;
import com.xiaoyong.entity.StatusCode;
import com.xiaoyong.system.pojo.Admin;
import com.xiaoyong.system.service.AdminService;
import com.xiaoyong.system.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @PostMapping("/login")//登录方法
    public Result login(@RequestBody Admin admin){
        boolean login = adminService.login(admin);
        if (login){//验证成功
            Map<String, String> info = new HashMap<>();
            info.put("username",admin.getLoginName());
            String token= JwtUtil.createJWT(UUID.randomUUID().toString(),admin.getLoginName(),null);
            info.put("token",token);
            return new Result(true,StatusCode.OK,"登录成功",info);
        }else {

            return new Result(false, StatusCode.LOGINERROR,"用户名或密码错误");
        }
    }




}
